package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.mapper.EmployeeMapper;
import cn.wolfcode.rbac.mapper.RoleMapper;
import cn.wolfcode.rbac.qo.EmployeeQo;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.web.exception.BusinessException;
import cn.wolfcode.rbac.web.vo.AdminStateVo;
import cn.wolfcode.rbac.web.vo.EmployeeRoleVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 员工业务
 */
@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageInfo<Employee> list(EmployeeQo employeeQo) {
        //设置页面
        PageHelper.startPage(employeeQo.getPageNum(),employeeQo.getPageSize());
        //查询所有员工信息
        List<Employee> employees = employeeMapper.selectAll(employeeQo);
        //把员工信息存入到PageInfo进行分页并返回
        PageInfo pageInfo = new PageInfo(employees);
        return pageInfo;
    }

    @Override
    public Employee get(Long id) {
        //根据员工id查询员工信息
        Employee employeeInfo = new Employee();
        employeeInfo.setId(id);
        Employee employee = employeeMapper.selectByEmployeeInfo(employeeInfo);
        if(employee == null){
            throw new BusinessException("没有该员工信息");
        }
        return employee;
    }

    @Override
    public void save(EmployeeRoleVo employeeRoleVo) {
        //保存员工信息
        int saveResult = employeeMapper.insert(employeeRoleVo.getEmployee());
        if(saveResult == 0){
            throw new BusinessException("员工信息保存失败");
        }

        //同时维护员工和角色的关系
        //获取保存成功后用户的Id
        Long saveId = employeeRoleVo.getEmployee().getId();
        //获取角色的id
        List<Long> roleIdList = employeeRoleVo.getRoleIdList();
        //保存员工和角色所对应的id
        //问题：此时，如果用遍历的方式去插入数据，则是有多少条数据，则发多少个请求
//        roleIdList.forEach(roleId->roleMapper.insertRoleIdAndEmployeeId(saveId,roleId));
        //优化方案：只需要发一次请求，在mybatis中用<foreach>动态标签插入即可
        roleMapper.insertRoleIdAndEmployeeId(saveId,roleIdList);
    }

    @Override
    public void update(EmployeeRoleVo employeeRoleVo) {
        //更新员工信息
        int updateResult = employeeMapper.updateByPrimaryKey(employeeRoleVo.getEmployee());
        if(updateResult == 0){
            throw new BusinessException("员工信息更新失败");
        }

        //同时维护员工和角色的关系
        Long updateEmployeeId = employeeRoleVo.getEmployee().getId();
        //根据员工的id,先删除员工的所有角色
        roleMapper.deleteRoleIdByEmployeeId(updateEmployeeId);
        //判断是否是超级管理员
        if(employeeRoleVo.getEmployee().getAdmin()){
            return;
        }
        //再添加新的角色
        //获取角色的id
        List<Long> roleIdList = employeeRoleVo.getRoleIdList();
        //保存员工和角色所对应的id
//问题：此时，如果用遍历的方式去插入数据，则是有多少条数据，则发多少个请求
//        roleIdList.forEach(roleId->roleMapper.insertRoleIdAndEmployeeId(updateEmployeeId,roleId));
        //优化方案：只需要发一次请求，在mybatis中用<foreach>动态标签插入即可
        roleMapper.insertRoleIdAndEmployeeId(updateEmployeeId,roleIdList);
    }

    @Override
    public void delete(Long id,Long userId) {
        //删除员工的信息，注意不能删除自己本身
        if(id == userId){
            throw new BusinessException("员工信息删除失败,不能删除本人");
        }
        int delEmployeeResult = employeeMapper.deleteByPrimaryKey(id);

        //维护员工和角色的关系，根据员工的id删除角色的信息
        int delRoleByEmployeeIdResult = roleMapper.deleteRoleIdByEmployeeId(id);
        if(delEmployeeResult == 0 || delEmployeeResult == 0){
            throw new BusinessException("员工信息删除失败");
        }
    }

    @Override
    public void updateState(AdminStateVo adminStateVo) {
        int updateAdminStateResult = employeeMapper.updateAdminState(adminStateVo);
        if(updateAdminStateResult == 0){
            throw new BusinessException("员工管理员状态更新失败");
        }
    }
}
